/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client.anim.sequence;

import gjt.Util;
import edu.idp.client.model.Cube3D;
import edu.idp.client.model.Model3D;

import java.awt.*;
import java.io.Serializable;

/**
 *
 * @author Kristopher T Babic
 */
public class ModelSequence extends Sequence implements Serializable {

    private float xrot = 0.0f;
    private float yrot = 0.0f;
    private float zrot = 0.0f;
    private float inc = 10.0f;
    private Dimension size;
    private Model3D model;
    private float xfac;
//  private Image     unfilteredImage = null;

    private static Model3D _defaultModel = new Cube3D();

    public ModelSequence() {
        this(_defaultModel);
    }

    public ModelSequence(Model3D model) {
        super();//c);

        initImages();
        this.model = model;

    }

    private void initTransVariables() {
        size = new Dimension(50, 50);

        float xw = model.getXMax() - model.getXMin();
        float yw = model.getYMax() - model.getYMin();
        float zw = model.getZMax() - model.getZMin();
        if (yw > xw)
            xw = yw;
        if (zw > xw)
            xw = zw;
        float f1 = size.width / xw;
        float f2 = size.height / xw;

        xfac = 0.6f * (f1 < f2 ? f1 : f2) * 1.0f;
    }

    public Image getCurrentImage() {

        if (currentImage == null) {
            initTransVariables();
            currentImage = myComp.createImage(size.width, size.height);
            Util.waitForImage(myComp, currentImage);
        }

        return currentImage;

    }

    public Image getFirstImage() {
        return getNextImage();
    }

    public Image getNextImage() {

        if (currentImage == null) {
            if (size == null)
                initTransVariables();
            currentImage = myComp.createImage(size.width, size.height);
        }

/*
		if (unfilteredImage == null){
      if (size == null)
        initTransVariables();
      unfilteredImage = myComp.createImage(size.width, size.height);
    }
 */

        model.setIdentity();
        model.setTranslate((float) (-(model.getXMin() + model.getXMax()) / 2.0f),
                           (float) (-(model.getYMin() + model.getYMax()) / 2.0f),
                           (float) (-(model.getZMin() + model.getZMax()) / 2.0f));

        model.setRotX(xrot);
        model.setRotY(yrot);
        model.setRotZ(zrot);

        model.setScale(xfac, -xfac, (float) (16.0 * xfac / size.width));
        model.setTranslate((float) (size.width / 2), (float) (size.height / 2), 0);
        model.setTransform();


        Graphics g = currentImage.getGraphics();

        //   int rgb = Color.red.getRGB();//myComp.getBackground().getRGB();

        g.setColor(myComp.getBackground());//new Color (rgb & 0x00ffffff));

        g.fillRect(0, 0, size.width, size.height);

/*
         Graphics g = unfilteredImage.getGraphics();

    g.setColor(Color.red);
    g.fillRect(0, 0, size.width, size.height);
*/

        model.paint(g);

//    currentImage = myComp.createImage(new FilteredImageSource(unfilteredImage.getSource(), new ColorFilter(Color.red.getRGB() & 0x00ffffff)));

        g.dispose();

        increment();

        lastImagePainted = currentImage;
        return currentImage;

    }

    public boolean isAtLastImage() {
        return false;
    }

    private void increment() {
        xrot = (xrot + inc) % 360.0f;
        zrot = (zrot + inc) % 360.0f;
        yrot = (yrot + inc) % 360.0f;
    }

    protected void initImages() {
        currentImage = null;
        lastImagePainted = null;
    }

    public String toString() {
        return "Animated 3D Rotating Model";
    }
/*
  private final class ColorFilter extends RGBImageFilter {
    private int transColor;
    public ColorFilter(int transColor){
      this.transColor = transColor;
      canFilterIndexColorModel = true;
    }

    public int filterRGB(int x, int y, int rgb) {
      if ((rgb & 0x00ffffff) == transColor)
        return rgb & 0x00ffffff;
      return rgb;
    }
  }
 */


}